package com.finalterm.order.feign;

import com.finalterm.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 商品服务降级处理
 */
@Slf4j
@Component
public class ProductServiceClientFallback implements ProductServiceClient {

    @Override
    public Result<Object> getProductDetail(Long productId) {
        log.warn("商品服务调用失败，执行降级逻辑，productId: {}", productId);
        return Result.error("商品服务不可用，请稍后重试");
    }

    @Override
    public Result<Boolean> deductStock(Object deductDTO) {
        log.warn("库存扣减服务调用失败，执行降级逻辑");
        return Result.error("库存服务不可用，请稍后重试");
    }

    @Override
    public Result<Boolean> recoverStock(Long productId, Integer quantity, String orderId) {
        log.warn("库存恢复服务调用失败，执行降级逻辑，productId: {}, quantity: {}, orderId: {}",
                productId, quantity, orderId);
        // 返回特殊错误码，表示需要补偿重试
        return Result.error(500, "库存恢复服务不可用，已记录补偿任务");
    }

    @Override
    public Result<Boolean> confirmStockDeduction(String orderNo, Long productId, Integer quantity, String orderStatus) {
        log.warn("库存确认服务调用失败，执行降级逻辑，orderNo: {}, productId: {}, quantity: {}, orderStatus: {}",
                orderNo, productId, quantity, orderStatus);
        // 库存确认失败不影响主流程，但需要记录日志
        return Result.error(500, "库存确认服务不可用，将稍后重试");
    }
}
